﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NFramework.ObjectModel
{
    public interface IAutoInstance<T>
    {
        T Instance { get; }
        bool HasInstance { get; }
    }

    public struct AutoInstance<T, TLoader> : IAutoInstance<T>
        where T : class
        where TLoader : T, new()
    {
        public T Instance
        {
            get
            {
                if (_instance == null) { _instance = new TLoader(); }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }
        private T _instance;

        public bool HasInstance
        {
            get
            {
                return (_instance != null);
            }
        }
    }

    public struct AutoInstance<T> : IAutoInstance<T>
        where T : class, new()
    {
        public T Instance
        {
            get
            {
                if (_instance == null) { _instance = new T(); }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }
        private T _instance;

        public bool HasInstance
        {
            get
            {
                return (_instance != null);
            }
        }
    }
}
